import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

@SuppressWarnings("serial")
public class TurnDeterminerGUI extends JPanel implements ActionListener {

	private int counter = 0;
	
	private Board board;
	private Player[] player;
	private Dice dice = new Dice();
	
	static JFrame frame;
	
	static JPanel dicePanel = new JPanel(new GridLayout(1, 2, 2, 5));
	static JPanel diceButtonPanel = new JPanel();
	static JPanel sidePanel = new JPanel(new GridLayout(0, 2, 2, 2));
	
	static JLabel[] diceLabel = new JLabel[2];
	static JLabel[] nameLabel;
	static JLabel[] rollLabel;
	
	static JButton rollButton;
	static JButton okayButton;
	static JButton cancelButton;
	
	public TurnDeterminerGUI(Board board) {
			
		super(new BorderLayout());
		
		this.board = board;
		this.player = new Player[board.getPlayerCount()];
		this.player = board.getPlayers();
		
		frame = new JFrame("Roll Dice");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setUndecorated(true);
		frame.setAlwaysOnTop(true);
		
		diceLabel[0] = new JLabel(createImageIcon(dice.getDiceURL(0)), SwingConstants.RIGHT);
		diceLabel[1] = new JLabel(createImageIcon(dice.getDiceURL(0)), SwingConstants.LEFT);
		
		dicePanel.add(diceLabel[0]);
		dicePanel.add(diceLabel[1]);
		
		nameLabel = new JLabel[board.getPlayerCount()+1];
		rollLabel = new JLabel[board.getPlayerCount()+1];
		
		nameLabel[0] = new JLabel("Player Name", JLabel.CENTER);
		rollLabel[0] = new JLabel("Roll", JLabel.CENTER);
		
		sidePanel.add(nameLabel[0]);
		sidePanel.add(rollLabel[0]);
		
		for (int x = 0; x < board.getPlayerCount(); x++) {
		
			nameLabel[x+1] = new JLabel(player[x].getName(), JLabel.CENTER);
			rollLabel[x+1] = new JLabel("-", JLabel.CENTER);
			
			sidePanel.add(nameLabel[x+1]);
			sidePanel.add(rollLabel[x+1]);
		}
		
		
		rollButton = new JButton("Roll Dice");
		rollButton.setMnemonic(KeyEvent.VK_M);
		rollButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	rollButtonActionPerformed(evt);
            }
        });
		
		okayButton = new JButton("Okay");
		okayButton.setEnabled(false);
		okayButton.setMnemonic(KeyEvent.VK_M);
		okayButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okayButtonActionPerformed(evt);
            }
        });
		
		cancelButton = new JButton("Cancel");
		cancelButton.setMnemonic(KeyEvent.VK_M);
		cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });
		
		rollButton.addActionListener(this);
		
		diceButtonPanel.add(rollButton);
		
		sidePanel.add(okayButton);
		sidePanel.add(cancelButton);
		
		frame.getContentPane().add(dicePanel, BorderLayout.PAGE_START);
		frame.getContentPane().add(diceButtonPanel);
		frame.getContentPane().add(sidePanel, BorderLayout.PAGE_END);
		
		frame.pack(); 
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
		
        setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30));
	}
	
	public void actionPerformed(ActionEvent arg0) {
		
	}

	private void rollButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
        
		dice = player[counter].initialRollDice();
		
		diceLabel[0].setIcon(createImageIcon(dice.getDiceURL(dice.getDice1()-1)));
		diceLabel[1].setIcon(createImageIcon(dice.getDiceURL(dice.getDice2()-1)));
		
		rollLabel[counter+1].setText(Integer.toString(dice.getSum()));
		
		diceLabel[0].repaint();
		diceLabel[1].repaint();
		rollLabel[counter+1].repaint();
		
		counter++;
		
		if (counter == board.getPlayerCount()) {
			
			board.sortPlayers();
			rollButton.setEnabled(false);
			okayButton.setEnabled(true);
		}
    }
	
	private void okayButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
        
		board.setLocation();
		
		new BoardGUI(board);
		frame.setVisible(false);
    }

	private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {                                         
    
		System.exit(0);
	}
	
	protected static ImageIcon createImageIcon(String path) {
		
        java.net.URL imgURL = BoardGUI.class.getResource(path);
		
        if (imgURL != null) {
		
            return new ImageIcon(imgURL);
        }
		
		else {
		
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }
}
